//
//  PersonDetailViewController.m
//  XRSquare
//
//  Created by UaenaMo on 2018/8/15.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "PersonDetailViewController.h"
#import "CustomNavigation.h"
#import "PersonTopTableViewCell.h"
#import "PersonContentTableViewCell.h"
#import "PersonContentDetailTableViewCell.h"
#import "PersonGrowViewController.h"
#import "PersonPubViewController.h"
#import "PersonPingjiaViewController.h"
#import "ChatViewController.h"

@interface PersonDetailViewController () <UITableViewDelegate, UITableViewDataSource> {
    UITableView *contentTableView;
    CustomNavigation *nav;
    UILabel *nameLabel;

    NSArray *data1;
    NSArray *data2;
    NSArray *data3;
    NSArray *data4;
    NSArray *data5;
    NSArray *data6;

    NSDictionary *contentData;

    NSString *name;
    NSString *avatar;
    NSString *descirption;
    NSString *age;
    NSString *evCount;
    NSString *abilityString;

    NSInteger isSelf;

    UIButton *fav;
    UIButton *add;
    UIButton *chat;
}
@end

@implementation PersonDetailViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        name = @"";
        avatar = @"";
        descirption = @"";
        age = @"";
        evCount = @"";
        abilityString = @"";

    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)backAction {
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];

    contentTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 50) style:UITableViewStylePlain];
    contentTableView.delegate = self;
    contentTableView.dataSource = self;
    contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (@available(iOS 11.0, *)) {
        contentTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
        contentTableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
    }
    [self.view addSubview:contentTableView];

    nav = [CustomNavigation navigation];
    nav.backgroundColor = [UIColor clearColor];
    [self.view addSubview:nav];

    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 44, 44)];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:@"Path"] forState:UIControlStateNormal];
    backButton.imageEdgeInsets = UIEdgeInsetsMake(11.5, 16, 11.5, 16);
    [nav addView:backButton];

    nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 20, ScreenWidth - 100, 44)];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.font = [UIFont boldSystemFontOfSize:18];
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.text = @"sdds";
    nameLabel.alpha = 0;
    [nav addView:nameLabel];

//    if (isSelf == 1) {
    add = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth - 100, 27, 30, 30)];
    [add setImage:[UIImage imageNamed:@"5511542797495_"] forState:UIControlStateNormal];
    [add addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
    add.hidden = YES;
    [nav addView:add];

    chat = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth - 70, 27, 30, 30)];
    [chat setImage:[UIImage imageNamed:@"chat"] forState:UIControlStateNormal];
    [chat addTarget:self action:@selector(chatAction) forControlEvents:UIControlEventTouchUpInside];
    chat.hidden = YES;
    [nav addView:chat];


    fav = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth - 40, 27, 30, 30)];
    [fav setImage:[UIImage imageNamed:@"collect"] forState:UIControlStateNormal];
    [fav setImage:[UIImage imageNamed:@"collected"] forState:UIControlStateSelected];
    [fav addTarget:self action:@selector(favAction) forControlEvents:UIControlEventTouchUpInside];
    fav.hidden = YES;
    [nav addView:fav];
//    }

    [self request];
}

- (void)request {
    [NetRequestCenter personalwithtaskid:self.uid result:^(BOOL success, NSDictionary *resultDic) {
        if (success) {
            NSLog(@"%@", resultDic);
            contentData = [resultDic objectForKey:@"data"];

            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            NSString *userInfo_UID = [NSString stringWithFormat:@"%@", [userDefaults objectForKey:@"UserInfo_UID"] ];
            NSLog(@"userinfo_uid:%@,--uid%@, --res:%@", userInfo_UID, self.uid, [userInfo_UID isEqual:self.uid]?@"=":@"!=");
            if ([userInfo_UID isEqual:self.uid]) {
                isSelf = 0;
            } else {
                isSelf = 1;
                fav.hidden = NO;
                add.hidden = NO;
                chat.hidden = NO;
            }

            name = [contentData objectForKey:@"Name"];
            age = [NSString stringWithFormat:@"%@", [contentData objectForKey:@"Age"]];
            avatar = [contentData objectForKey:@"HeadPic"];
            evCount = [NSString stringWithFormat:@"%@", [contentData objectForKey:@"EvaluateNumber"]];
            descirption = [contentData objectForKey:@"Intro"];
            NSArray *abilityContent = [contentData objectForKey:@"abilityName"];
            if ([abilityContent count]) {
                abilityString = [abilityContent firstObject];
                for (int i = 1; i < [abilityContent count]; i++) {
                    abilityString = [NSString stringWithFormat:@"%@\n%@", abilityString, [abilityContent objectAtIndex:i]];
                }
            }

            if ([[NSString stringWithFormat:@"%@", [contentData objectForKey:@"FavoriteStatus"]] isEqualToString:@"0"]) {
                fav.enabled = NO;
            } else if ([[NSString stringWithFormat:@"%@", [contentData objectForKey:@"FavoriteStatus"]] isEqualToString:@"1"]) {
                fav.enabled = YES;
                fav.selected = YES;
            } else {
                fav.enabled = YES;
                fav.selected = NO;
            }


            if ([[contentData objectForKey:@"GRDSJ"] isKindOfClass:[NSDictionary class]]) {
                data1 = @[[contentData objectForKey:@"GRDSJ"]];
            } else {
                data1 = @[];
            }
            if ([[contentData objectForKey:@"GRFC"] isKindOfClass:[NSDictionary class]]) {
                data2 = @[[contentData objectForKey:@"GRFC"]];
            } else {
                data2 = @[];
            }
            if ([[contentData objectForKey:@"XYHD"] isKindOfClass:[NSDictionary class]]) {
                data3 = @[[contentData objectForKey:@"XYHD"]];
            } else {
                data3 = @[];
            }
            if ([[contentData objectForKey:@"SHHD"] isKindOfClass:[NSDictionary class]]) {
                data4 = @[[contentData objectForKey:@"SHHD"]];
            } else {
                data4 = @[];
            }
            if ([[contentData objectForKey:@"JNZZ"] isKindOfClass:[NSDictionary class]]) {
                data5 = @[[contentData objectForKey:@"JNZZ"]];
            } else {
                data5 = @[];
            }
            if ([[contentData objectForKey:@"MXWT"] isKindOfClass:[NSDictionary class]]) {
                data6 = @[[contentData objectForKey:@"MXWT"]];
            } else {
                data6 = @[];
            }
            [contentTableView reloadData];

            nameLabel.text = name;
        } else {
            [SVProgressHUD showErrorWithStatus:[resultDic objectForKey:@"message"]];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

- (void)addAction {
    [SVProgressHUD show];
    [NetRequestCenter makefriendWithpage:nil friendname:nil uid:self.uid result:^(BOOL success, NSDictionary *resultDic) {
        if (success) {
            [SVProgressHUD showSuccessWithStatus:@"添加成功"];
        } else {
            [SVProgressHUD showErrorWithStatus:[resultDic objectForKey:@"message"]];
        }
    }];
}

- (void)chatAction {
    self.navigationController.navigationBar.hidden = NO;

    ChatViewController *chat = [[ChatViewController alloc] init];
    chat.contentid = [[contentData objectForKey:@"privately"] objectForKey:@"groupID"];
    chat.targetName = [[contentData objectForKey:@"privately"] objectForKey:@"RightName"];
    chat.targetUid = self.uid;
    chat.targetPic = [[contentData objectForKey:@"privately"] objectForKey:@"RightHeadPic"];
    [self.navigationController pushViewController:chat animated:YES];
}

- (void)favAction {
    if (!fav.selected) {
        [SVProgressHUD show];
        [NetRequestCenter favoritepeopleuid:self.uid result:^(BOOL success, NSDictionary *resultDic) {
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"收藏成功"];
                self->fav.selected = !self->fav.selected;
            } else {
                [SVProgressHUD showErrorWithStatus:[resultDic objectForKey:@"message"]];
            }
        }];
    } else {
        [SVProgressHUD show];
        [NetRequestCenter delfavoritepeopleuid:self.uid result:^(BOOL success, NSDictionary *resultDic) {
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"取消收藏成功"];
                self->fav.selected = !self->fav.selected;
            } else {
                [SVProgressHUD showErrorWithStatus:[resultDic objectForKey:@"message"]];
            }
        }];
    }
}

#pragma mark - tableview

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y < 150) {
        nameLabel.alpha = scrollView.contentOffset.y / 150;
        nav.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0 alpha:scrollView.contentOffset.y / 150];
    } else {
        nav.backgroundColor = themeColor;
        nameLabel.alpha = 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return [PersonTopTableViewCell height];
    } else if (indexPath.row == 1) {
        return [PersonContentTableViewCell height];
    } else if (indexPath.row == 2) {
        return [PersonContentDetailTableViewCell heightWithDetail:age];
    } else if (indexPath.row == 3) {
        return [PersonContentDetailTableViewCell heightWithDetail:descirption];
    } else if (indexPath.row == 4) {
        return [PersonContentDetailTableViewCell heightWithDetail:abilityString];
    }
//    else if (indexPath.row == 5)
//    {
//        return [PersonContentTableViewCell height];
//    }
    else if (indexPath.row == 6) {
        return [PersonContentTableViewCell height];
    } else if (indexPath.row == 7) {
        return [PersonContentTableViewCell height];
    } else if (indexPath.row == 8) {
        return [PersonContentTableViewCell height];
    } else {
        return [PersonContentTableViewCell height];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        static NSString *identifer = @"sd";
        PersonTopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[PersonTopTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer];
        }
        [cell loadimage:avatar name:name age:age description:descirption];
        return cell;
    } else if (indexPath.row == 1 || indexPath.row == 5 || indexPath.row == 6 || indexPath.row == 7 || indexPath.row == 8 || indexPath.row == 9) {
        static NSString *identifer = @"sd";
        PersonContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[PersonContentTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer];
        }
        if (indexPath.row == 1) {
            [cell loadTitle:@"成长档案" arrowImage:nil];
        }
//        else if (indexPath.row == 5)
//        {
//            [cell loadTitle:@"个人档案" arrowImage:nil];
//        }
        else if (indexPath.row == 6) {
            [cell loadTitle:@"技能售卖" arrowImage:nil];
        } else if (indexPath.row == 7) {
            [cell loadTitle:@"需求发布" arrowImage:nil];
        } else if (indexPath.row == 8) {
            [cell loadTitle:@"活动" arrowImage:nil];
        } else {
            [cell loadTitle:@"共0条评价" arrowImage:nil];
        }
        return cell;
    } else {
        static NSString *identifer = @"sd";
        PersonContentDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[PersonContentDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer];
        }
        if (indexPath.row == 2) {
            [cell loadData:age title:@"年龄"];
        } else if (indexPath.row == 3) {
            [cell loadData:descirption title:@"简介"];
        } else {
            [cell loadData:abilityString title:@"技能"];
        }
        return cell;
    }

}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1 || indexPath.row == 5 || indexPath.row == 6 || indexPath.row == 7 || indexPath.row == 8 || indexPath.row == 9) {
        return YES;
    } else {
        return NO;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.navigationController.navigationBar.hidden = NO;

    if (indexPath.row == 1) {
        PersonGrowViewController *persongrow = [[PersonGrowViewController alloc] init];
        persongrow.type = @"0";
        persongrow.contentId = self.uid;
        persongrow.companyName = name;
        // 个人主页， 成长档案  跳转  contentTargetselector 的值控制PersonGrowViewController 的标题。
        persongrow.contentTargetselector = @"person_select";
        persongrow.logoUrl = avatar;
        [self.navigationController pushViewController:persongrow animated:YES];
    }
//    else if (indexPath.row == 5)
//    {
//        PersonGrowViewController * persongrow = [[PersonGrowViewController alloc]init];
//        persongrow.type = @"6";
//        persongrow.contentId = self.uid;
//        persongrow.companyName = name;
//        persongrow.logoUrl = avatar;
//        [self.navigationController pushViewController:persongrow animated:YES];
//    }
    else if (indexPath.row == 6) {
        PersonPubViewController *person = [[PersonPubViewController alloc] init];
        person.contentId = self.uid;
        person.preselect = 1;
        person.other = 0;
        [self.navigationController pushViewController:person animated:YES];
    } else if (indexPath.row == 7) {
        PersonPubViewController *person = [[PersonPubViewController alloc] init];
        person.contentId = self.uid;
        person.preselect = 2;
        person.other = 0;
        [self.navigationController pushViewController:person animated:YES];
    } else if (indexPath.row == 8) {
        PersonPubViewController *person = [[PersonPubViewController alloc] init];
        person.contentId = self.uid;
        person.preselect = 3;
        person.other = 0;
        [self.navigationController pushViewController:person animated:YES];
    } else if (indexPath.row == 9) {
        PersonPingjiaViewController *pingjia = [[PersonPingjiaViewController alloc] init];
        pingjia.contentId = self.uid;
        [self.navigationController pushViewController:pingjia animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
